package com.easy;

import java.util.Arrays;

/**
 * Created by Lxk on 2019/8/17.
 */
public class Solution905 {

    public int[] sortArrayByParity(int[] A) {
        if (A == null || A.length == 0) {
            return null;
        }
        int left = -1, right = A.length;
        while (true) {
            //找到左边的奇数
            while (A[++left] % 2 == 0) {
                if (left == A.length - 1) {
                    break;
                }
            }
            //找到右边的偶数
            while (A[--right] % 2 == 1) {
                if (right == 0) {
                    break;
                }
            }
            //判断指针位置是否正常
            if (right <= left) {
                break;
            }
            //交换位置
            int temp = A[left];
            A[left] = A[right];
            A[right] = temp;
        }
        sortArr(A, 0, left);
        sortArr(A, left, A.length);
        return A;
    }

    void sortArr(int[] arr, int begin, int end) {
        for (int i = begin; i < end; i++) {
            for (int k = begin; k < end - (i - begin) - 1; k++) {
                if (arr[k] < arr[k + 1]) {
                    int temp = arr[k];
                    arr[k] = arr[k + 1];
                    arr[k + 1] = temp;
                }
            }
        }
    }

    public static void main(String[] args) {
//        int[] arr = {3, 1, 2, 4, 5};
        int[] arr = {0};
        Solution905 solution905 = new Solution905();
        arr = solution905.sortArrayByParity(arr);
        Arrays.stream(arr).forEach(e -> System.out.print(e + " "));
        System.out.println();
    }
}
